import axios from 'axios';
import router from '../router';

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            // 'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Type': 'application/json'
        },
        data: params,
        traditional: true
    }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json',
            token: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true,
        /* transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ] */
    }).then(res => {
        if(res.data.code==90002){
            localStorage.removeItem("userInfo")
            localStorage.removeItem('logintoken')
        }
        return res.data;
    });
};

export {
    loginreq,
    req
}